using HerenityCode.Reference.GO;
using UnityEngine;

namespace HerenityCode.Tool
{
    namespace GO
    {
        /// <summary>
        /// 挂载在需要移动的物体上
        /// </summary>
        public class AxialMotion : MonoBehaviour
        {
            [HideInInspector] public GameObject moveObject;
            private GameObjectMessage msg = new GameObjectMessage();

            public float offset = 0.02f;
            public string str = "Mouse Y";
            public Vector3 direction = new Vector3(1, 1, 1).normalized;
            private GameObject moveTarget;

            public string xyz = "y";
            public Vector3 minXYZ;
            public Vector3 maxXYZ;

            void Update()
            {
                SelectObject();
                MoveAndRotateObject();
                if (Input.GetMouseButtonUp(0))
                {
                    moveTarget = null;
                }
            }

            /// <summary>
            /// 传入直线和物体
            /// </summary>
            /// <param name="moveObject">物体</param>
            /// <param name="offset">灵敏度，数值越大物体移动幅度越大</param>
            /// <param name="direction">物体移动的正方向，需要一个归一值</param>
            /// <param name="mouse">Mouse Y或者Mouse X，默认Mouse Y</param>
            public void SetAxisAndObject(GameObject moveObject, float offset, Vector3 direction,
                string mouse = "Mouse Y")
            {
                this.offset = offset;
                this.direction = direction;
                this.moveObject = moveObject;
                str = mouse;
            }

            /// <summary>
            /// 传入大致轴向和限制点
            /// </summary>
            /// <param name="xyz">哪个轴被限制了</param>
            /// <param name="min">被限制的轴值较小的点</param>
            /// <param name="max">被限制的轴值较大的点</param>
            public void SetLimitX(string xyz, Vector3 min, Vector3 max)
            {
                this.xyz = xyz;
                minXYZ = min;
                maxXYZ = max;
            }

            //清空直线和物体
            public void ClearAxisAndObject()
            {
                offset = 0;
                direction = Vector3.zero;
                moveObject = null;
                str = null;
            }

            //清空大致轴向和限制点
            public void ClearLimitX()
            {
                xyz = "";
                minXYZ = Vector3.zero;
                maxXYZ = Vector3.zero;
            }

            // 选择物体
            private void SelectObject()
            {
                msg = GetGameObject.MouseClickObject("Drag");

                if (msg.TarGO == moveObject)
                {
                    moveTarget = msg.TarGO;
                }
            }

            //移动物体和判断界限
            private void MoveAndRotateObject()
            {
                if (moveTarget == null) return;
                if (Input.GetMouseButton(0))
                {
                    if (xyz == "x")
                    {
                        if (moveTarget.transform.localPosition.x > maxXYZ.x)
                        {
                            moveTarget.transform.localPosition = maxXYZ;
                            return;
                        }
                        else if (moveTarget.transform.localPosition.x < minXYZ.x)
                        {
                            moveTarget.transform.localPosition = minXYZ;
                            return;
                        }
                    }
                    else if (xyz == "y")
                    {
                        if (moveTarget.transform.localPosition.y > maxXYZ.y)
                        {
                            moveTarget.transform.localPosition = maxXYZ;
                            return;
                        }
                        else if (moveTarget.transform.localPosition.y < minXYZ.y)
                        {
                            moveTarget.transform.localPosition = minXYZ;
                            return;
                        }
                    }
                    else if (xyz == "z")
                    {
                        if (moveTarget.transform.localPosition.z > maxXYZ.z)
                        {
                            moveTarget.transform.localPosition = maxXYZ;
                            return;
                        }
                        else if (moveTarget.transform.localPosition.z < minXYZ.z)
                        {
                            moveTarget.transform.localPosition = minXYZ;
                            return;
                        }
                    }

                    float mouseX = Input.GetAxis(str);
                    moveTarget.transform.Translate(direction * mouseX * offset, Space.Self);
                }
            }
        }
    }
}